<template>
    <div class="main-contain">
      <div id="admin">
        <div class="pos" v-loading="state.loading">
          <p class="adminh1">非遗京剧京剧平台</p>
          <p class="adminh3">用户登录</p>
          <el-form ref="formRef" label-width="80px" :status-icon="true" :model="login" :rules="rules" @keyup.enter="onLogin">
            <el-form-item prop="mobile" label="手机号">
              <el-input v-model="login.mobile" placeholder="用户名" prefix-icon="user" autocomplete="off" size="large"></el-input>
            </el-form-item>
            <el-form-item prop="pwd" class="label" label="密码">
              <el-input placeholder="密码" v-model="login.pwd" prefix-icon="lock" autocomplete="off" size="large" show-password></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Unlock" @click="onLogin" style="background-color:#2a5caa;">登录</el-button>
              <el-button type="success" @click="toReg()" icon="EditPen" style="background-color:#c63c26;">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { CacheToken } from "@/constants/cacheKey";
import baseService from "@/service/baseService";
import { setCache } from "@/utils/cache";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { useStore } from 'vuex';

const store = useStore();

const router = useRouter();

const state = reactive({
  loading: false
});

const login = reactive({ mobile: "13760613080", pwd: "" });

const formRef = ref();

const rules = ref({
  mobile: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  pwd: [{ required: true, message: "必填项不能为空", trigger: "blur" }]
});

/**
 * 登录
 */
const onLogin = () => {
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      state.loading = true;
      baseService
        .post("/api/user/login", login).then((res) => {
          state.loading = false;
          if (res.code === 200) {
            setCache(CacheToken, res.data);
            ElMessage.success("登录成功");
            store.commit('setFrontLoginUser', res.data);//使用Vuex保存登录用户
            router.push("/opera/index");
            // router.push({path:'/sys/user',query:{}})
          } else {
            ElMessage.error(res.msg);
          }
        }).catch((err) => {
          console.error(err)
          state.loading = false;
        });
    }
  });
};

const toReg = ()=>{
  router.push("/user/register");
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
body {
  background: rgb(135, 206, 235);
}
.main-contain{
  width: 100%;
  height: 100vh;
  background-image: url('../../../assets/images/bg.jpg');
  background-repeat: no-repeat;
  background-size: cover;
}
#admin {
  color: #333;
  position: absolute;
  top: 60px;
  left: 30%;
  width: 40%;
  padding: 15px 5%;
  height: auto;
  background: rgba(255,255,255,.8);
  border-radius: 10px;
  box-shadow: 8px 10px 10px rgba(0,0,0,.3);
  border: 1px solid #2a5caa;
}
.adminh1 {
  font-size: 22px;
  margin: 20px 0;
  text-align: center;
  color: #333;
}
.login-form {
  margin: 0 auto;
  display: block;
  width: 420px;
}
#admin .el-form-item__content{
  margin: 0;
  text-align: center;
}
.adminh3{
  font-size: 16px;
  margin: 10px 0 50px;
  text-align: center;
  color: #333;
}
.label{
  color:#000 !important;
}
</style> 